import sys
from collections import defaultdict

sys.setrecursionlimit(1000000)
n, m = map(int, input().split())
c = [0] + list(map(int, input().split()))
d = [0] + list(map(int, input().split()))
c_edges = defaultdict(list)
d_edges = defaultdict(list)

for _ in range(n - 1):
    u, v = map(int, input().split())
    if u > v:
        u, v = v, u
    c_edges[u].append(v)
for _ in range(m - 1):
    u, v = map(int, input().split())
    if u > v:
        u, v = v, u
    d_edges[u].append(v)

ans = 0


def dfs(c_i, d_i, cnt):
    global ans
    ans = max(ans, cnt + 1)
    for i in c_edges[c_i]:
        for j in d_edges[d_i]:
            if c[i] != d[j]: continue
            dfs(i, j, cnt + 1)


dfs(1, 1, 0)
print(ans)
